import threading
import time

import psutil
from flask import jsonify, current_app
from flask_socketio import emit

from create_app import redis_pool
from . import bg_bp

# event = threading.Event()
#
#
# def task():
#     pid = None
#     while True:
#         event.wait()
#         try:
#             if not pid:
#                 pid = int(redis_pool.get('opcua_server_pid').decode('utf8'))
#             process = psutil.Process(pid)
#             status = {
#                 'pid': pid,
#                 'name': process.name(),
#                 'status': process.status(),
#             }
#             print(status)
#             emit('message', status)
#         except Exception as e:
#             emit('message', str(e))
#             print(e)
#         time.sleep(1)
#
#
# @bg_bp.route('/start')
# def task_start():
#     threading.Thread(target=task, daemon=True).start()
#     event.set()
#     return jsonify({'status': 'ok'})
#
#
# @bg_bp.route('/stop')
# def task_stop():
#     event.clear()
#     return jsonify({'status': 'ok'})
